package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.MonitorProduct;
import com.banksoft.cps.bean.common.ErrorTip;
import com.banksoft.cps.bean.query.MonitorProductQuery;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.enums.Status;
import com.banksoft.cps.service.MonitorProductService;
import com.banksoft.cps.service.impl.MonitorProductServiceImpl;
import com.banksoft.cps.util.PageUtils;
import com.banksoft.cps.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2017/12/29.
 */
@Controller
@RequestMapping("/admin/mProduct/")
public class AdminMonitorProductController extends ControllerConstants {

    @Autowired
    MonitorProductService monitorProductService;

    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(MonitorProductQuery query){
        List<MonitorProduct> list =  monitorProductService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(MonitorProductQuery query){
        long count =  monitorProductService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        MonitorProduct bean = monitorProductService.findById(id);
        return toJSONString(createDataTip(bean));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(MonitorProduct bean, HttpServletRequest request){
        try {
            monitorProductService.save(bean);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }
    @RequestMapping(value = "listAll", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String listAll(){
        MonitorProductQuery query = new MonitorProductQuery();
        query.setStatus(Status.valid.name());
        List<MonitorProduct> list =  monitorProductService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(String ids){
        if(StringUtil.isNullOrEmpty(ids)){
            return toJSONString(new ErrorTip(1,"请选择要删除的记录"));
        }
        monitorProductService.delete(ids.split(","));
        return toJSONString(SUCCESS);
    }

}
